<%@ page import="java.util.List" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.text.DateFormat" %>
<%@ page import="org.tigr.htc.persist.*" %>
<%@ page import="org.tigr.htc.common.*" %>
<%@ page import="org.tigr.htc.cmd.*" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="org.apache.log4j.PropertyConfigurator" %>


<%!
    static String[] colors = new String[] { "#F0F0F0", "#c0c0f0" };
    static String where = "(state = \"" + CmdState.WAITING + "\" OR state = \"" + 
    							CmdState.RUNNING + "\" OR state = \"" + 
    							CmdState.INIT + "\")" + " AND (type != \"internal\")";
    static String order = "userName";
	static private Logger logger = Logger.getLogger(Command.class);
  	DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
%>
<html>
<head>
<title>HTC User Summary</title>
</head>

<body>
<%@ include file="header.jsp"%>
<%-- <jsp:include page="header.jsp" flush="true">
</jsp:include> --%>
<h2 align="center"><font face="arial,helvetica,sanserif" >HTCService Request Summary</font></h2>
<br>
<table border=0 cellspacing=0 cellpadding=4 align="center">
<tr>
<th><font>Username</font></th>
<th><font>Queued</font></th>
<th><font>Waiting</font></th>
<th><font>Running</font></th>
</tr>

<%
	int totInit = 0, totWait = 0, totRun = 0; // Vars to hold the total
	
	// Retrieve all the commands that are in the INIT, WAITING, and RUNNING
	// state to display the overall stats
	// Trying to see if getting infor directly from the database
	// solves the problem with incorrect state
    // IPersist pl = PersistFactory.getIPersist();
    IPersist pl = DirectDatabase.getInstance();
    
  	List list = pl.searchCommands(where, order, null);
  	Iterator iter = list.iterator();
  	
  	// Build the hash of users based
  	Hashtable usersHash = new Hashtable();
  	
  	Command cmd;
	String prevUser = "";
	Hashtable statusHash = null;
	int init = 0, running = 0, waiting = 0; 
	for (int i = 0; iter.hasNext(); i++) {
    	cmd = (Command) iter.next();
    	
    	// If the user name for this command is different then create a new entry
    	// in the hash for the new user
    	String user = cmd.getUserName();
    	if (!user.equals(prevUser)) {
    		// If this is not the first record and the user name has changed
    		// then set the status for the three states also update the total count
    		if (i != 0) {
    			// Set the totals for this user
    			statusHash.put(CmdState.INIT, new Integer(init));
    			statusHash.put(CmdState.WAITING, new Integer(waiting));
    			statusHash.put(CmdState.RUNNING, new Integer(running));
    			
    			// Update total
    			totInit += init;
    			totRun += running;
    			totWait += waiting;
    		}
    		
    		// Create nre users hash for the new user
    		prevUser = user;
    		statusHash = new Hashtable();
    		usersHash.put(user, statusHash);
    			
  			// Reset the user counts
   			init = 0; running = 0; waiting = 0;
    	}
 
 		// Based on the state of this command update the count
 		if (cmd.getState() == CmdState.INIT) {
 			init++;
 		}
 		else  if (cmd.getState() == CmdState.WAITING) {
 			waiting++;
 		}
 		else  if (cmd.getState() == CmdState.RUNNING) {
 			running++;
 		} 	
    	
    }
    
    // At the end of the loop the values for the last user need to be put to the hash
    // so do so
    
    if (list.size() > 0) {
	    // Set the totals for this user
    	statusHash.put(CmdState.INIT, new Integer(init));
    	statusHash.put(CmdState.WAITING, new Integer(waiting));
    	statusHash.put(CmdState.RUNNING, new Integer(running));
    	
    			
    	// Update total
    	totInit += init;
    	totRun += running;
    	totWait += waiting;
	}
	
	// Loop through the users hash and show data for each user
	int i = 0;
	for (iter = usersHash.keySet().iterator(); iter.hasNext(); i++) {
		String user = (String) iter.next();
		statusHash = (Hashtable) usersHash.get(user);
%>

<%--  Alternate the color of the background --%>
<tr bgcolor="<%= colors[(i % 2)] %>">
<td><%= user %></td>
<td align="right"><%= statusHash.get(CmdState.INIT)%></td>
<td align="right"><%= statusHash.get(CmdState.WAITING)%></td>
<td align="right"><%= statusHash.get(CmdState.RUNNING)%></td>
</tr>

<% } %>

<tr  bgcolor="black">
<td><font color="white"><b>Total</b></font></td>
<td align="right"><font color="white"><b><%= totInit %></b></font></td>
<td align="right"><font color="white"><b><%= totWait %></b></font></td>
<td align="right"><font color="white"><b><%= totRun %></b></font></td>
</tr>
<tr>
<td colspan="100">
<font size="small">
Queued - Queued on HTC Server<br>
Waiting - Waiting on the Grid<br>
Running - Running on the Grid
</font>
</td>
</tr>
</table>
<br>

<jsp:include page="footer.jsp" flush="true"></jsp:include>

</body>
</html>
